import axios from 'axios';
import { Message } from 'element-ui';
//process.env.VUE_APP_BASE_API
const service = axios.create({
    baseURL: 'https://www.xiaohaoo.com/api/8020', // url = base url + request url
});

service.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        const res = response.data;
        if (res.code !== 200) {
            Message({
                message: res.message || '出错了！',
                type: 'error',
                duration: 3 * 1000
            });
            return Promise.reject(new Error(res.message || '出错了！'));
        } else {
            return res;
        }
    },
    error => {
        console.log('err' + error); // for debug
        Message({
            message: error.message || '出错了！',
            type: 'error',
            duration: 3 * 1000
        });
        return Promise.reject(error);
    }
);

export default service;
